package com.huaguoguo.cyg.spider.processor.tl;

import cn.hutool.core.util.StrUtil;
import com.huaguoguo.cyg.entity.tl.to.Gang;
import com.huaguoguo.cyg.entity.tl.to.Player;
import com.huaguoguo.cyg.spider.parse.TLRankParser;
import com.huaguoguo.cyg.spider.processor.BasePageProcessor;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.nodes.Document;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;

import java.util.List;

/**
 * 类说明： 玩家战力排行榜爬取<br/>
 *
 * @author: huaguoguo
 * Date: 2019/8/17
 * Time: 14:53
 */
@Slf4j
public class TLPalyerRankProcessor extends BasePageProcessor {

    private String urlTemplate = "http://tl.changyou.com/tlbbrank/{}/top50zhanli.html";
    private String url;

    @Override
    public void process(Page page) {
        // xsoup
        Document document = page.getHtml().getDocument();
        List<Player> players = TLRankParser.parsePlayerRank(document,data);
        // 类型标识
        page.putField("flag","player");
        page.putField("players",players);
    }

    @Override
    public Site getSite() {
        return super.getSite().setCharset("gbk");
    }

    /**
     * 构建一个完整的请求链接
     *
     * @return
     */
    @Override
    public String buildRequestUrl() {
        this.url = StrUtil.format(urlTemplate, data.get("areaId"));
        return url;
    }
}
